import Icon from '@/components/Icon';
import classNames from 'classnames';
import { ReactNode } from 'react';
import { useHistory } from 'react-router-dom';
import styles from './index.module.scss';

type NavbarType = {
  onBack?: () => void;
  children?: ReactNode;
  right?: ReactNode;
  fixed?: boolean;
};

export default function NavBar({ onBack, children, right, fixed }: NavbarType) {
  const history = useHistory();
  const hanleBack = () => {
    if (onBack) {
      return onBack();
    }
    history.goBack();
  };
  return (
    <div className={styles.root}>
      {/* 动态添加fixed变为可选属性,使navbar固定在顶部 */}
      <div className={classNames('main', { fixed })}>
        <div className="left">
          <Icon type="iconfanhui" onClick={hanleBack} />
        </div>
        <div className="title">{children}</div>
        <div className="right">{right}</div>
      </div>
    </div>
  );
}
